package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.cgbl.cg.databinding.AppFragmentRecommendBinding
import com.cgbl.cg.ui.WebActivity
import com.cgbl.cg.view.ReportPop
import com.cgbl.cg.vm.home.HomeRecommendViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.pic.ImageLoaderUtils
import com.robot.base.view.DividerItemDecoration
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class HomeRecommendFragment :
    GLBaseFragment<AppFragmentRecommendBinding, HomeRecommendViewModel>() {
    private lateinit var bannerTitleAdapter: BannerImageAdapter<BannerBean>
    private lateinit var recommendCourseAdapter: RecommendCourseAdapter
    private lateinit var classicCaseAdapter: ClassicCaseAdapter
    private lateinit var popularMaterialAdapter: PopularMaterialAdapter
    private lateinit var workAdapter: WorkAdapter
    private lateinit var discountAdapter: DiscountAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_recommend

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initView()
        initBanner()
    }

    override fun initData() {
        super.initData()
        viewModel.refresh()
    }

    private fun initBanner() {
        bannerTitleAdapter = object : BannerImageAdapter<BannerBean>(
            arrayListOf()
        ) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: BannerBean?,
                position: Int,
                size: Int
            ) {
                ImageLoaderUtils.getGlideManager()
                    .load(data?.imagesFile)
                    .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
                    .into(holder?.imageView!!)
            }
        }
        binding.banner.setAdapter(bannerTitleAdapter)
        binding.banner.setIndicator(binding.indicator, false)
        binding.banner.setBannerGalleryEffect(12, 4)
        binding.banner.start()

        bannerTitleAdapter.setOnBannerListener { data, position ->
            WebActivity.start(requireContext(), data.hrefUrl ?: "", data?.coverName ?: "")
        }
    }

    private fun initView() {
        recommendCourseAdapter = RecommendCourseAdapter()
        binding.recyclerViewCourse.apply {
            adapter = recommendCourseAdapter
        }

        classicCaseAdapter = ClassicCaseAdapter()

        binding.recyclerViewClassicCase.apply {
            adapter = classicCaseAdapter
            addItemDecoration(
                DividerItemDecoration(
                    requireContext(),
                    LinearLayoutManager.VERTICAL,
                    0.5f,
                    R.color.app_F7F6FA
                )
            )
        }


        popularMaterialAdapter = PopularMaterialAdapter()
        binding.recyclerViewPopularMaterial.apply {
            adapter = popularMaterialAdapter
        }

        discountAdapter = DiscountAdapter(
            arrayListOf()
        )
        binding.bannerDiscount.setAdapter(
            discountAdapter
        )

        binding.bannerDiscount.setBannerGalleryEffect(84, 0)
        binding.bannerDiscount.start()

        workAdapter = WorkAdapter()
        binding.recyclerViewWork.apply {
            adapter = workAdapter
        }

        workAdapter.addChildClickViewIds(R.id.item_iv_more)
        workAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_iv_more -> {
                    val pop = ReportPop(requireContext())
                    XPopup.Builder(requireContext())
                        .asCustom(pop)
                        .show()
                    pop.setCancelClickListener {
                        pop.dismiss()
                    }
                    pop.setConfirmClickListener {
                        pop.dismiss()
                        ARouterUtil.goToActivityWithBundle(
                            AppConstants.Router.Main.A_REPORT,
                            Bundle().apply {
                                putString("id", workAdapter.data[position].accountId)
                            }
                        )
                    }
                }
            }
        }

    }

    override fun initViewObservable() {
        super.initViewObservable()

//        binding.smartRefreshLayout.setOnLoadMoreListener {
//            it.finishLoadMore()
//            viewModel.toWorkCommand.execute()
//        }

        binding.smartRefreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }

        viewModel.uc.refreshEvent.observe(this) {
            binding.smartRefreshLayout.finishRefresh()
        }

        viewModel.uc.courseDataEvent.observe(this) {
            recommendCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
        }

        viewModel.uc.workDataEvent.observe(this) {
            workAdapter.setNewInstance(it as MutableList<WorksBean>?)
        }

        viewModel.uc.ribbonDataEvent.observe(this) {
            bannerTitleAdapter.setDatas(it)
        }

        viewModel.uc.limitedDataEvent.observe(this) {
            discountAdapter.setDatas(it)
        }

        viewModel.uc.projectDataEvent.observe(this) {
            classicCaseAdapter.setNewInstance(it as MutableList<ProjectBean>?)
        }

        viewModel.uc.resourceDataEvent.observe(this) {
            popularMaterialAdapter.setNewInstance(it as MutableList<ResourceBean>?)
        }
    }
}